package core;

import java.util.ArrayList;

public class ClusteredBallList {
	private int left;
	private int right;
	private int top;
	private int bottom;
	private double centerX;
	private double centerY;
	
	private ArrayList<Ball> ballList;
	
	public ArrayList<Ball> getBallList() {
		return ballList;
	}
	public ClusteredBallList(Ball ball) {
		this.left = ball.getLeft();
		this.right = ball.getRight();
		this.top = ball.getTop();
		this.bottom = ball.getBottom();
		this.centerX = ball.getCenterX();
		this.centerY = ball.getCenterY();
		
		this.ballList = new ArrayList<Ball>();
		ballList.add(ball);
	}
	public ClusteredBallList(int leftX, int rightX, int topY, int bottomY, ArrayList<Ball> ballList) {
		super();
		this.left = leftX;
		this.right = rightX;
		this.top = topY;
		this.bottom = bottomY;
		this.ballList = ballList;
	}

	public int getLeft() {
		return left;
	}
	public int getRight() {
		return right;
	}
	public int getTop() {
		return top;
	}
	public int getBottom() {
		return bottom;
	}
	public boolean isInRangeY(Ball ball) {
		return ( ball.getCenterY() <= this.top + 30 && ball.getCenterY() >= this.bottom - 30);
	}
	public int compareY(Ball ball) {
		if ( ball.getCenterY() > this.centerY ) return 1;
		else if ( ball.getCenterY() == this.centerY ) return 0;
		else return -1;
	}
	public void addBall(Ball ball) {
		this.left = (this.left + ball.getLeft())/2;
		this.right = (this.right + ball.getRight())/2;
		this.top = (this.top + ball.getTop())/2;
		this.bottom = (this.left + ball.getBottom())/2;
		this.centerX = (this.centerX + ball.getCenterX())/2;
		this.centerY = (this.centerY + ball.getCenterY())/2;
		
		for(int i=0; i < ballList.size(); i++) {
			if(ball.compareTo(ballList.get(i)) < 0) {
				ballList.add(i, ball);
				break;
			} else if (i == ballList.size() -1) {
				ballList.ensureCapacity(1);
				ballList.add(ball);
				break;
			}
		}
	}
}
